package com.b2.service.impl;

import com.b2.constant.MessageConstant;
import com.b2.context.BaseContext;
import com.b2.mapper.BookCategoryMapper;
import com.b2.mapper.BookMapper;
import com.b2.mapper.WebUserMapper;
import com.b2.pojo.dto.BookCategoryDTO;
import com.b2.pojo.entity.Book;
import com.b2.pojo.entity.BookCategory;
import com.b2.pojo.entity.WebUser;
import com.b2.result.Result;
import com.b2.service.BookCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 悦神
* @description 针对表【book_category】的数据库操作Service实现
* @createDate 2024-05-27 10:28:35
*/
@Service
public class BookCategoryServiceImpl extends ServiceImpl<BookCategoryMapper, BookCategory>
    implements BookCategoryService{
    @Autowired
    BookCategoryMapper bookCategoryMapper;
    @Autowired
    BookMapper bookMapper;
    @Autowired
    private WebUserMapper webUserMapper;

    @Override
    public Result<List<BookCategory>> check() {
        Long userId = BaseContext.getCurrentId();
        WebUser webUser = webUserMapper.selectById(userId);
        LambdaQueryWrapper<BookCategory> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (webUser.getUserRole().equals(0)){
            objectLambdaQueryWrapper.eq(StringUtils.isNotBlank(userId.toString()),BookCategory::getUserId,userId);
        }
        List<BookCategory> bookCategories = bookCategoryMapper.selectList(objectLambdaQueryWrapper);
        return Result.success(bookCategories);
    }
    @Transactional
    @Override
    public Result delete(Long id) {
        LambdaQueryWrapper<Book> bookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookLambdaQueryWrapper.eq(Book::getBookCategoryId,id);
        List<Book> books = bookMapper.selectList(bookLambdaQueryWrapper);
        if (books.size()>0) {
            throw new RuntimeException(MessageConstant.BOOK_CATEGORY_DELETE_FAIL);
        }
        bookCategoryMapper.deleteById(id);
        return Result.success();
    }
    @Transactional
    @Override
    public Result<BookCategoryDTO> add(String name) {


        BookCategory bookCategory = new BookCategory();
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<BookCategory> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(BookCategory::getName,name);
        List<BookCategory> bookCategories = bookCategoryMapper.selectList(objectLambdaQueryWrapper);
        if (bookCategories.size()>0) {
            throw new RuntimeException(MessageConstant.NAME_REPEAT);
        }
        bookCategory.setName(name);
        bookCategory.setUserId(userId);
        bookCategoryMapper.insert(bookCategory);
        return Result.success();
    }
}




